#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
@File    :   hownet_process.py    
@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2022/4/22 0022 11:13   st      1.0         None
'''
import os
import time

import OpenHowNet
# OpenHowNet.download()
from process.data_export_process import data_export_hownet
from utils.constent import hownet_dir

# OpenHowNet.download()
hownet_dict_advanced = OpenHowNet.HowNetDict(init_sim=True)
hownet_sim_threshold = 0.8


def get_hownet_sim(word_1, word_2):
    # t1 = time.time()
    sim = hownet_dict_advanced.calculate_word_similarity(word_1, word_2)
    if sim == -1:
        sim = 0
    # print('-----hownet-sim:', time.time() - t1)
    return sim


def get_hownet_dict():

    hownet_dict_path = os.path.join(hownet_dir, 'HowNet.txt')
    datas = []
    temp_dict = dict()
    for l in open(hownet_dict_path, 'r', encoding='utf-8'):
        l = l.strip()
        if not l:
            datas.append(temp_dict)
            temp_dict = dict()
            continue
        ls = l.split('=')
        key = ls[0]
        value = l[l.index('=')+1:].replace('\x15', ' ')
        temp_dict[key] = value
    if temp_dict.keys():
        datas.append(temp_dict)
    s_dict = dict()
    for dt in datas:
        id = dt['NO.']
        S_C = dt['S_C']
        S_E = dt['S_E']
        s_dict[id] = (S_C, S_E)

    all_senses = hownet_dict_advanced.get_all_senses()
    data_export_hownet(all_senses, s_dict)
    return datas


if __name__ == '__main__':
    # all_sememes = hownet_dict_advanced.get_all_sememes()
    # all_senses = hownet_dict_advanced.get_all_senses()
    # for senses in all_senses:
    #     id = senses.No
    #     if id == '000000026417':
    #         print(senses)
    # print(all_senses)
    get_hownet_dict()
